﻿
/* ==============================================================================
*
*   Copyright (c) 2010, Pierre Favier
*   All rights reserved.
*
*   NTasking is free software: you can redistribute it and/or modify it
*   under the terms of the GNU General Public License as published by the
*   Free Software Foundation.
*
*   Redistribution and use in source and binary forms, with or without
*   modification, are permitted provided that the following conditions
*   are met:
*
*   -   Redistributions of source code must retain the above copyright notice,
*       this list of conditions and the following disclaimer.
*
*   -   Redistributions in binary form must reproduce the above
*       copyright notice, this list of conditions and the following
*       disclaimer in the documentation and/or other materials
*       provided with the distribution.
*
*   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
*   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
*   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
*   BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
*   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
*   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
*   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
*   ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
*   TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
*   THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
*   SUCH DAMAGE.
*
* ============================================================================== */

// This code has been generated by the T4 engine. DO NOT EDIT otherwise your changes
// will be overwritten at next generation


namespace NTasking.Internals
{

    /// <summary>
    /// Define the context of a synchronized method call, i.e. its calling parameters and result.
    /// </summary>
    internal abstract class CallContext : ContextBase
    {

        private readonly int _parametersCount;


        /// <summary>
        /// Gets the number of parameters.
        /// </summary>
        /// <value>The number of parameters.</value>
        internal int ParametersCount
        {
            get { return _parametersCount; }
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="CallContext"/> class.
        /// </summary>
        /// <param name="parametersCount">The number of parameters of the new instance.</param>
        protected CallContext(int parametersCount)
        {
            _parametersCount = parametersCount;
        }


        /// <summary>
        /// Sets the result's value.
        /// </summary>
        /// <typeparam name="ResultT">The type of the result value.</typeparam>
        /// <param name="val">The value of the result.</param>
        internal abstract void SetResult<ResultT>(ResultT val);


        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return ToString(ParametersCount);
        }


        /// <summary>
        /// Returns a <see cref="System.String"/> that represents the parameter values of this instance.
        /// </summary>
        /// <param name="count">The count.</param>
        /// <returns>
        /// A <see cref="System.String"/> that represents the parameter values of this instance.
        /// </returns>
        internal string ToString(int count)
        {
            return "(" + ParameterValues(count) + ")";
        }


        /// <summary>
        /// Returns a <see cref="System.String"/> that represents the values of the count-first parameter.
        /// </summary>
        /// <param name="count">The number of parameters to be formatted.</param>
        /// <returns>A <see cref="System.String"/> that represents the count-first parameter values of this instance.</returns>
        protected virtual string ParameterValues(int count)
        {
            return "";
        }

    }


    /// <summary>
    /// Represents the CallContext passed to synchronized method calls with no parameters
    /// </summary>
    /// <typeparam name="RetT">The type of the method's result.</typeparam>
    internal class CallContext<RetT> : CallContext, IResult<RetT>
    {

        private const int Level = 0;
        private RetT _result;
        private static readonly bool _isVoid = typeof(RetT) == typeof(Void);


        /// <summary>
        /// Gets or sets the result of the call.
        /// </summary>
        /// <value>The result of the call.</value>
        internal RetT Result
        {
            get
            {
                CheckResult();
                return _result;
            }
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="CallContext{RetT}"/> class.
        /// </summary>
        internal CallContext()
            : this(Level)
        {
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="CallContext{RetT}"/> class.
        /// </summary>
        /// <param name="count">The number of parameters of the new instance.</param>
        protected CallContext(int count): base(count)
        {
        }


        /// <summary>
        /// Sets the result value.
        /// </summary>
        /// <param name="value">The result.</param>
        void IResult<RetT>.Set(RetT value)
        {
            _result = value;
            SetResult();
        }


        /// <summary>
        /// Sets the result's value.
        /// </summary>
        /// <typeparam name="ResultT">The type of the result value.</typeparam>
        /// <param name="val">The value of the result.</param>
        internal override void SetResult<ResultT>(ResultT val)
        {
            if (_isVoid)
                SetResult();
            else
                ((IResult<ResultT>)this).Set(val);
        }

    }
    
    
    /// <summary>
    /// Represents the parameters passed to synchronized method calls with 1 parameters
    /// </summary>
    /// <typeparam name="P1T">The type of parameter 1</typeparam>
    /// <typeparam name="RetT">The type of the method's result.</typeparam>
    internal class CallContext<P1T, RetT> : CallContext<RetT>, IParameters<P1T>    {

        private const int Level = 1;
        private readonly P1T _p1;


        /// <summary>
        /// Gets the value of parameter 1.
        /// </summary>
        /// <value>The value of parameter 1.</value>
        public P1T P1 { get { return _p1; } }


        /// <summary>
        /// Initializes a new instance of the <see cref="CallContext{P1T, RetT}"/> class.
        /// </summary>
        /// <param name="p1">The value of the parameter 1.</param>
        internal CallContext(P1T p1)
            : this(Level, p1)
        {
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="CallContext{P1T, RetT}"/> class.
        /// </summary>
        /// <param name="count">The number of parameters of this instance.</param>
        /// <param name="p1">The value of the parameter 1.</param>
        protected CallContext(int count, P1T p1)
            : base(count)
        {
            _p1 = p1;
        }


        /// <summary>
        /// Returns a <see cref="System.String"/> that represents the values of the count-first parameter.
        /// </summary>
        /// <param name="count">The count of parameters to be retrieved.</param>
        /// <returns>
        /// A <see cref="System.String"/> that represents the values of the count-first parameter.
        /// </returns>
        protected override string ParameterValues(int count)
        {
            if (count >= Level)
                try
                {
                    return P1.ToString();
                }
                catch
                {
                    return "???";
                }

            return base.ParameterValues(count);
        }

    }


    /// <summary>
    /// Represents the parameters passed to synchronized method calls with 2 parameters
    /// </summary>
    /// <typeparam name="P1T">The type of parameter 1</typeparam>
    /// <typeparam name="P2T">The type of parameter 2</typeparam>
    /// <typeparam name="RetT">The type of the method's result.</typeparam>
    internal class CallContext<P1T, P2T, RetT> : CallContext<P1T, RetT>, IParameters<P1T, P2T>    {

        private const int Level = 2;
        private readonly P2T _p2;


        /// <summary>
        /// Gets the value of parameter 2.
        /// </summary>
        /// <value>The value of parameter 2.</value>
        public P2T P2 { get { return _p2; } }


        /// <summary>
        /// Initializes a new instance of the <see cref="CallContext{P1T, P2T, RetT}"/> class.
        /// </summary>
        /// <param name="p1">The value of the parameter 1.</param>
        /// <param name="p2">The value of the parameter 2.</param>
        internal CallContext(P1T p1, P2T p2)
            : this(Level, p1, p2)
        {
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="CallContext{P1T, P2T, RetT}"/> class.
        /// </summary>
        /// <param name="count">The number of parameters of this instance.</param>
        /// <param name="p1">The value of the parameter 1.</param>
        /// <param name="p2">The value of the parameter 2.</param>
        protected CallContext(int count, P1T p1, P2T p2)
            : base(count, p1)
        {
            _p2 = p2;
        }


        /// <summary>
        /// Returns a <see cref="System.String"/> that represents the values of the count-first parameter.
        /// </summary>
        /// <param name="count">The count of parameters to be retrieved.</param>
        /// <returns>
        /// A <see cref="System.String"/> that represents the values of the count-first parameter.
        /// </returns>
        protected override string ParameterValues(int count)
        {
            if (count >= Level)
                try
                {
                    return base.ParameterValues(count) + ", " + P2;
                }
                catch
                {
                    return "???";
                }

            return base.ParameterValues(count);
        }

    }


    /// <summary>
    /// Represents the parameters passed to synchronized method calls with 3 parameters
    /// </summary>
    /// <typeparam name="P1T">The type of parameter 1</typeparam>
    /// <typeparam name="P2T">The type of parameter 2</typeparam>
    /// <typeparam name="P3T">The type of parameter 3</typeparam>
    /// <typeparam name="RetT">The type of the method's result.</typeparam>
    internal class CallContext<P1T, P2T, P3T, RetT> : CallContext<P1T, P2T, RetT>, IParameters<P1T, P2T, P3T>    {

        private const int Level = 3;
        private readonly P3T _p3;


        /// <summary>
        /// Gets the value of parameter 3.
        /// </summary>
        /// <value>The value of parameter 3.</value>
        public P3T P3 { get { return _p3; } }


        /// <summary>
        /// Initializes a new instance of the <see cref="CallContext{P1T, P2T, P3T, RetT}"/> class.
        /// </summary>
        /// <param name="p1">The value of the parameter 1.</param>
        /// <param name="p2">The value of the parameter 2.</param>
        /// <param name="p3">The value of the parameter 3.</param>
        internal CallContext(P1T p1, P2T p2, P3T p3)
            : this(Level, p1, p2, p3)
        {
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="CallContext{P1T, P2T, P3T, RetT}"/> class.
        /// </summary>
        /// <param name="count">The number of parameters of this instance.</param>
        /// <param name="p1">The value of the parameter 1.</param>
        /// <param name="p2">The value of the parameter 2.</param>
        /// <param name="p3">The value of the parameter 3.</param>
        protected CallContext(int count, P1T p1, P2T p2, P3T p3)
            : base(count, p1, p2)
        {
            _p3 = p3;
        }


        /// <summary>
        /// Returns a <see cref="System.String"/> that represents the values of the count-first parameter.
        /// </summary>
        /// <param name="count">The count of parameters to be retrieved.</param>
        /// <returns>
        /// A <see cref="System.String"/> that represents the values of the count-first parameter.
        /// </returns>
        protected override string ParameterValues(int count)
        {
            if (count >= Level)
                try
                {
                    return base.ParameterValues(count) + ", " + P3;
                }
                catch
                {
                    return "???";
                }

            return base.ParameterValues(count);
        }

    }


    /// <summary>
    /// Represents the parameters passed to synchronized method calls with 4 parameters
    /// </summary>
    /// <typeparam name="P1T">The type of parameter 1</typeparam>
    /// <typeparam name="P2T">The type of parameter 2</typeparam>
    /// <typeparam name="P3T">The type of parameter 3</typeparam>
    /// <typeparam name="P4T">The type of parameter 4</typeparam>
    /// <typeparam name="RetT">The type of the method's result.</typeparam>
    internal class CallContext<P1T, P2T, P3T, P4T, RetT> : CallContext<P1T, P2T, P3T, RetT>, IParameters<P1T, P2T, P3T, P4T>    {

        private const int Level = 4;
        private readonly P4T _p4;


        /// <summary>
        /// Gets the value of parameter 4.
        /// </summary>
        /// <value>The value of parameter 4.</value>
        public P4T P4 { get { return _p4; } }


        /// <summary>
        /// Initializes a new instance of the <see cref="CallContext{P1T, P2T, P3T, P4T, RetT}"/> class.
        /// </summary>
        /// <param name="p1">The value of the parameter 1.</param>
        /// <param name="p2">The value of the parameter 2.</param>
        /// <param name="p3">The value of the parameter 3.</param>
        /// <param name="p4">The value of the parameter 4.</param>
        internal CallContext(P1T p1, P2T p2, P3T p3, P4T p4)
            : this(Level, p1, p2, p3, p4)
        {
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="CallContext{P1T, P2T, P3T, P4T, RetT}"/> class.
        /// </summary>
        /// <param name="count">The number of parameters of this instance.</param>
        /// <param name="p1">The value of the parameter 1.</param>
        /// <param name="p2">The value of the parameter 2.</param>
        /// <param name="p3">The value of the parameter 3.</param>
        /// <param name="p4">The value of the parameter 4.</param>
        protected CallContext(int count, P1T p1, P2T p2, P3T p3, P4T p4)
            : base(count, p1, p2, p3)
        {
            _p4 = p4;
        }


        /// <summary>
        /// Returns a <see cref="System.String"/> that represents the values of the count-first parameter.
        /// </summary>
        /// <param name="count">The count of parameters to be retrieved.</param>
        /// <returns>
        /// A <see cref="System.String"/> that represents the values of the count-first parameter.
        /// </returns>
        protected override string ParameterValues(int count)
        {
            if (count >= Level)
                try
                {
                    return base.ParameterValues(count) + ", " + P4;
                }
                catch
                {
                    return "???";
                }

            return base.ParameterValues(count);
        }

    }

}
